/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

/**
 *
 * @author Sasha Rakic
 */
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

public class Zahtev {
    private String username;
    private String password;
    private String ime;
    private String prezime;
    private String email;
    private String funkcija ;
    private Timestamp vreme;

    public Zahtev(String u,String p,String i,String pr,String e,String f,Timestamp t){
        username=u;
        password=p;
        ime=i;
        prezime=pr;
        email=e;
        funkcija=f;
        vreme=t;
    }

     public String odbaci(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        String vrati;
        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis());

            String upit = "update zahtevi_registracije set vreme_odluke ='"+ now +"',"+
                        "status ='"+ 2 +"'"+
                        "where username = '" + username + "';";
            stmt.executeUpdate(upit);

            ArrayList<Zahtev> reqs = (ArrayList<Zahtev>)session.getAttribute("zahtevi");
            for(int i=0;i<reqs.size();i++)
                if( ((Zahtev)reqs.get(i)).equals(this) ){
                    reqs.remove(i);
                    i--;
                }
            session.setAttribute("zahtevi", reqs);

            stmt.close();con.close();
        }catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {}
            }
            context.addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            vrati="error";
            return vrati;
        }
        User u=(User)session.getAttribute("user");
        return u.apregledzahteva();
     }

     public String odobri(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        String vrati;
        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            int isAdmin=0;
            if(funkcija.equalsIgnoreCase("administrator") || funkcija.equalsIgnoreCase("администратор"))
                isAdmin=1;



            String upit = "INSERT INTO korisnik (username, lozinka, ime, prezime, email, funkcija, isAdmin) VALUES "
                        + "('" + username + "' , '"+ password + "' , '" + ime + "' , '" + prezime + "' , '" + email + "' , '"+ funkcija + "' , '" + isAdmin + "');";
            stmt.executeUpdate(upit);

            Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis());

                upit = "update zahtevi_registracije set vreme_odluke ='"+ now +"',"+
                        "status ='"+ 1 +"'"+
                        "where username = '" + username + "';";
            stmt.executeUpdate(upit);

            ArrayList<Zahtev> reqs = (ArrayList<Zahtev>)session.getAttribute("zahtevi");
            for(int i=0;i<reqs.size();i++)
                if( ((Zahtev)reqs.get(i)).equals(this) ){
                    reqs.remove(i);
                    i--;
                }
            session.setAttribute("zahtevi", reqs);

            stmt.close();con.close();
        }catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {}
            }
            context.addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            vrati="error";
            return vrati;
        }
        User u=(User)session.getAttribute("user");
        return u.apregledzahteva();
     }

    //GETTERS AND SETTERS
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the ime
     */
    public String getIme() {
        return ime;
    }

    /**
     * @param ime the ime to set
     */
    public void setIme(String ime) {
        this.ime = ime;
    }

    /**
     * @return the prezime
     */
    public String getPrezime() {
        return prezime;
    }

    /**
     * @param prezime the prezime to set
     */
    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the funkcija
     */
    public String getFunkcija() {
        return funkcija;
    }

    /**
     * @param funkcija the funkcija to set
     */
    public void setFunkcija(String funkcija) {
        this.funkcija = funkcija;
    }

    /**
     * @return the vreme
     */
    public Timestamp getVreme() {
        return vreme;
    }

    /**
     * @param vreme the vreme to set
     */
    public void setVreme(Timestamp vreme) {
        this.vreme = vreme;
    }

}
